<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>刷题语言基础用法 - Team317</title>
	<script>(function(d,e){d[e]=d[e].replace("no-js","js");})(document.documentElement,"className");</script>
	<meta name="description" content="">
		<meta property="og:title" content="刷题语言基础用法" />
<meta property="og:description" content="刷题建议
多练简单题，多练简单题，多练简单题。
中难题前期没必要练，因为基础不够，算法方面做好长期处于初级阶段的觉悟，因为算法的思路需要积累，不是学完数据结构就可以。
简单题也能提供很多解题技巧，把这些技巧学会了，再来挑战中难题也就有底子了。
前期挑战中难题浪费时间，而且心态容易崩。" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://team317.gitee.io/%E7%AE%97%E6%B3%95/%E5%88%B7%E9%A2%98%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E7%94%A8%E6%B3%95/" /><meta property="article:section" content="算法" />
<meta property="article:published_time" content="2021-08-20T00:00:00+00:00" />
<meta property="article:modified_time" content="2021-08-20T00:00:00+00:00" />


	<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
	<link rel="dns-prefetch" href="//fonts.googleapis.com">
	<link rel="dns-prefetch" href="//fonts.gstatic.com">
	<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700">

	<link rel="stylesheet" href="/css/style.css">
	

	<link rel="shortcut icon" href="/favicon.ico">
		
</head>
<body class="body">
	<div class="container container--outer">
		<header class="header">
	<div class="container header__container">
		
	<div class="logo logo--mixed">
		<a class="logo__link" href="/" title="Team317" rel="home">
			<div class="logo__item logo__imagebox">
					<img class="logo__img" src="/img/_mountain%20climbing.png">
				</div><div class="logo__item logo__text">
					<div class="logo__title">Team317</div>
					<div class="logo__tagline">欢迎访问我的博客空间</div>
				</div>
		</a>
	</div>
		<div class="divider"></div>
	</div>
</header>
		<div class="wrapper flex">
			<div class="primary">
			
<main class="main" role="main">
	<article class="post">
		<header class="post__header">
			<h1 class="post__title">刷题语言基础用法</h1>
			<div class="post__meta meta">
<div class="meta__item-datetime meta__item">
	<svg class="meta__icon icon icon-time" width="16" height="14" viewBox="0 0 30 28"><path d="M15 0C7 0 1 6 1 14s6 14 14 14 14-6 14-14S23 0 15 0zm0 25C9 25 4 20 4 14S9 3 15 3s11 5 11 11-5 11-11 11zm1-18h-2v8.4l6.8 4.4L22 18l-6-3.8V7z"/></svg><time class="meta__text" datetime="2021-08-20T00:00:00Z">August 20, 2021</time></div><div class="meta__item-categories meta__item"><svg class="meta__icon icon icon-category" width="16" height="16" viewBox="0 0 16 16"><path d="m7 2l1 2h8v11h-16v-13z"/></svg><span class="meta__text"><a class="meta__link" href="/categories/%E7%AE%97%E6%B3%95/" rel="category">算法</a>
	</span>
</div></div>
		</header>
		<div class="content post__content clearfix">
			<p><strong>刷题建议</strong></p>
<p>多练简单题，多练简单题，多练简单题。</p>
<p>中难题前期没必要练，因为基础不够，算法方面做好长期处于初级阶段的觉悟，因为算法的思路需要积累，不是学完数据结构就可以。</p>
<p>简单题也能提供很多解题技巧，把这些技巧学会了，再来挑战中难题也就有底子了。</p>
<p>前期挑战中难题浪费时间，而且心态容易崩。</p>
<h2 id="语言基础">语言基础</h2>
<h3 id="c基本操作">C++基本操作</h3>
<h4 id="1容器">1、容器</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c++" data-lang="c++">vector<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">int</span><span style="color:#f92672">&gt;</span> arr;		<span style="color:#75715e">// 定义容器
</span><span style="color:#75715e"></span>vector<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">int</span><span style="color:#f92672">&gt;</span> arr(<span style="color:#ae81ff">9</span>);		<span style="color:#75715e">// 定义大小为9的容器
</span><span style="color:#75715e"></span>vector<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">int</span><span style="color:#f92672">&gt;</span> arr(<span style="color:#ae81ff">9</span>,<span style="color:#ae81ff">0</span>);	<span style="color:#75715e">// 定义大小为9，初始值为0的容器,leetcode不支持
</span><span style="color:#75715e"></span>arr.size();				<span style="color:#75715e">// 获取容器的大小
</span><span style="color:#75715e"></span>arr.push_back(x);		<span style="color:#75715e">// 将值x放入容器末尾
</span><span style="color:#75715e"></span>vector<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">int</span><span style="color:#f92672">&gt;</span> arr[<span style="color:#ae81ff">9</span>];		<span style="color:#75715e">// 定义一个长度为9的vector&lt;int&gt;数组，数组的每个元素都是vector&lt;int&gt;
</span><span style="color:#75715e"></span>
</code></pre></div><p>定义容器：vector&lt;int&gt; arr</p>
<p>容器长度：arr.size()</p>
<p>向容器中添加元素：arr.push_back(element)</p>
<h4 id="2栈">2、栈</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c++" data-lang="c++">stack<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">int</span><span style="color:#f92672">&gt;</span> st;	<span style="color:#75715e">// 定义栈方法
</span><span style="color:#75715e"></span>st.empty();		<span style="color:#75715e">// 判断栈空，为空时返回true
</span><span style="color:#75715e"></span>st.push(x);		<span style="color:#75715e">// 将值x压入栈顶
</span><span style="color:#75715e"></span>st.top();		<span style="color:#75715e">// 返回栈顶元素
</span><span style="color:#75715e"></span>st.pop();		<span style="color:#75715e">// 弹出栈顶元素
</span></code></pre></div><h4 id="3队列">3、队列</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-C++" data-lang="C++">queue<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">int</span><span style="color:#f92672">&gt;</span> Q;	<span style="color:#75715e">// 定义队列
</span><span style="color:#75715e"></span>Q.push(x);		<span style="color:#75715e">// 将x压入队尾
</span><span style="color:#75715e"></span>Q.pop();		<span style="color:#75715e">// 弹出但不返回队尾元素
</span><span style="color:#75715e"></span>Q.front();		<span style="color:#75715e">// 获取对头元素
</span><span style="color:#75715e"></span>Q.back();		<span style="color:#75715e">// 获取队尾元素
</span><span style="color:#75715e"></span>Q.empty();		<span style="color:#75715e">// 判断队列是否为空
</span><span style="color:#75715e"></span>Q.size();		<span style="color:#75715e">// 获取队列大小
</span><span style="color:#75715e"></span>
</code></pre></div><h4 id="4字符串">4、字符串</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-C++" data-lang="C++">string str<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;123&#34;</span>;		<span style="color:#75715e">// 定义字符串
</span><span style="color:#75715e"></span>strlen(str);			<span style="color:#75715e">// 获取字符串长度
</span><span style="color:#75715e"></span>str.size();				<span style="color:#75715e">// 获取字符串长度
</span><span style="color:#75715e"></span>Integer.parseInt(str);	<span style="color:#75715e">// 将数字字符串转化为数字
</span><span style="color:#75715e"></span>str.substr(index,len);	<span style="color:#75715e">// 获取str中从index开始，长为len的子串	
</span></code></pre></div><p>注意，isnumber()不是标准的C/C++库函数。</p>
<p>如果刷题时没有提供字符串转化为数字的函数，则需要自定义一个函数完成这一功能，在编写时需要注意处理负号。</p>
<h4 id="5慎用mallochttpswwwcnblogscomfriedcoderp13098677html">5、<a href="https://www.cnblogs.com/friedCoder/p/13098677.html">慎用malloc</a></h4>
<p>malloc是C语言中常用的内存申请方式，C++也支持，但是对于STL来说却不兼容，取而代之的是new。</p>
<p>情景：曾写过一个C与C++混合的项目，内存申请使用了malloc，同时使用了std::vector，程序中两种结构之间进行了数据流通。但是程序运行时，部分指针的值被“莫名修改”，导致运行出错。</p>
<p>使用gdb调试，观察相关变量的内存地址，发现std::vector竟然可以覆盖修改malloc此前申请的内存区域。明显std::vector<strong>不识别</strong>malloc申请的内存，将这部分内存标记为<strong>未使用</strong>状态。</p>
<p>解决方法：将所有malloc改为new，解决问题。</p>
<p>今天在leetcode中刷题用C++解题时，使用了malloc来申请空间，虽然过程没问题，但提交后不能通过。发现在本地运行能出正确结果，这才定位到是malloc的问题。</p>
<h4 id="6哈希表">6、哈希表</h4>
<p>在处理存在重复值的序列中作用较大。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c++" data-lang="c++">unordered_set<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">int</span><span style="color:#f92672">&gt;</span> hash;	<span style="color:#75715e">// 定义一个哈希表
</span><span style="color:#75715e"></span>hash.insert(x);				<span style="color:#75715e">// 将x加入到哈希表中
</span><span style="color:#75715e"></span>hash.find(x);				<span style="color:#75715e">// 判断x是否在hash中
</span><span style="color:#75715e">// find(x)返回等于end()表示没找到
</span><span style="color:#75715e"></span>hash.find(x) <span style="color:#f92672">==</span> hash.end();	
hash.erase(x);				<span style="color:#75715e">// 清除元素x
</span><span style="color:#75715e"></span>hash.clear(x);				<span style="color:#75715e">// 清除所有元素
</span><span style="color:#75715e"></span>hash.count(x);				<span style="color:#75715e">// 统计hash表中x的个数
</span></code></pre></div><h4 id="7排序">7、排序</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c++" data-lang="c++">vector<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">int</span><span style="color:#f92672">&gt;</span> nums;
sort(nums.begin(), nums.end());	<span style="color:#75715e">// 排序
</span></code></pre></div><h4 id="8位运算">8、位运算</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c++" data-lang="c++">x <span style="color:#f92672">=</span> x<span style="color:#f92672">^</span>x;	<span style="color:#75715e">// 按位异或，可检测两个值是否相等
</span><span style="color:#75715e"></span>x <span style="color:#f92672">^=</span> x;		<span style="color:#75715e">// 同上，具有交换律，即a^b^c = a^c^b
</span></code></pre></div><p>异或运算的特点：a^b = c ， a^b^b = a， 即 c^b=a 同理 c^a =b</p>
<h4 id="9基本类型">9、基本类型</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-c++" data-lang="c++"><span style="color:#66d9ef">int</span> max <span style="color:#f92672">=</span> INT32_MAX;	<span style="color:#75715e">// 整型的最大值
</span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> min <span style="color:#f92672">=</span> INT32_MIN;	<span style="color:#75715e">// 整型的最小值
</span></code></pre></div><h3 id="c基本操作-1">C基本操作</h3>
<h4 id="1数组">1、数组</h4>
<p><strong>在函数中返回数组：</strong></p>
<p>需要注意的是待返回的数组不能是函数中的局部变量，如：char str[100];</p>
<p>若 return str，则返回后数组str所在的空间会被回收；</p>
<p>定义时应改写为char *str = (char **)malloc(sizeof(char)*100);</p>
<h3 id="java基本操作">Java基本操作</h3>
<h4 id="1队列">1、队列</h4>
<p>​	LinkedList 实现了 Queue 接口，可作为队列使用。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">Queue<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">int</span><span style="color:#f92672">[]&gt;</span> q <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> LinkedList<span style="color:#f92672">&lt;&gt;();</span>	<span style="color:#75715e">// 定义队列
</span><span style="color:#75715e"></span>q<span style="color:#f92672">.</span><span style="color:#a6e22e">add</span><span style="color:#f92672">(</span>3<span style="color:#f92672">);</span>		<span style="color:#75715e">// 向队尾添加元素；
</span><span style="color:#75715e"></span>q<span style="color:#f92672">.</span><span style="color:#a6e22e">poll</span><span style="color:#f92672">();</span>		<span style="color:#75715e">// 删除队首元素并返回该元素，队列为空时返回null；
</span><span style="color:#75715e"></span>q<span style="color:#f92672">.</span><span style="color:#a6e22e">isEmpty</span><span style="color:#f92672">();</span>	<span style="color:#75715e">// 判断队列是否为空
</span></code></pre></div><h4 id="2链表节点">2、链表节点</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#75715e">// java中定义链表节点
</span><span style="color:#75715e"></span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">ListNode</span> <span style="color:#f92672">{</span>
	<span style="color:#66d9ef">int</span> val<span style="color:#f92672">;</span>
    ListNode next<span style="color:#f92672">;</span>
    ListNode<span style="color:#f92672">()</span> <span style="color:#f92672">{}</span>
    ListNode<span style="color:#f92672">(</span><span style="color:#66d9ef">int</span> val<span style="color:#f92672">)</span> <span style="color:#f92672">{</span> <span style="color:#66d9ef">this</span><span style="color:#f92672">.</span><span style="color:#a6e22e">val</span> <span style="color:#f92672">=</span> val<span style="color:#f92672">;</span> <span style="color:#f92672">}</span>
    ListNode<span style="color:#f92672">(</span><span style="color:#66d9ef">int</span> val<span style="color:#f92672">,</span> ListNode next<span style="color:#f92672">)</span> <span style="color:#f92672">{</span> <span style="color:#66d9ef">this</span><span style="color:#f92672">.</span><span style="color:#a6e22e">val</span> <span style="color:#f92672">=</span> val<span style="color:#f92672">;</span> <span style="color:#66d9ef">this</span><span style="color:#f92672">.</span><span style="color:#a6e22e">next</span> <span style="color:#f92672">=</span> next<span style="color:#f92672">;</span> <span style="color:#f92672">}</span>
 <span style="color:#f92672">}</span>
<span style="color:#75715e">// 判断节点的后继是否为空
</span><span style="color:#75715e"></span>ListNode node <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> ListNode<span style="color:#f92672">();</span>
<span style="color:#66d9ef">if</span><span style="color:#f92672">(</span>node<span style="color:#f92672">.</span><span style="color:#a6e22e">next</span> <span style="color:#f92672">==</span> <span style="color:#66d9ef">null</span><span style="color:#f92672">){;}</span>

</code></pre></div><h4 id="3数组arraylistlist">3、数组、ArrayList、List</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#75715e">// 定义一个数组
</span><span style="color:#75715e"></span>Integer<span style="color:#f92672">[</span>6<span style="color:#f92672">]</span> arr <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> Integer<span style="color:#f92672">[];</span>
<span style="color:#75715e">// 将数组转为ArrayList
</span><span style="color:#75715e"></span>List<span style="color:#f92672">&lt;</span>Integer<span style="color:#f92672">&gt;</span> arr1 <span style="color:#f92672">=</span> Arrays<span style="color:#f92672">.</span><span style="color:#a6e22e">asList</span><span style="color:#f92672">(</span>arr<span style="color:#f92672">);</span>

<span style="color:#75715e">// 定义一个ArrayList
</span><span style="color:#75715e"></span>List<span style="color:#f92672">&lt;</span>Integer<span style="color:#f92672">&gt;</span> temp <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> ArrayList<span style="color:#f92672">&lt;&gt;();</span>
<span style="color:#75715e">// 向数组中添加元素x
</span><span style="color:#75715e"></span>temp<span style="color:#f92672">.</span><span style="color:#a6e22e">add</span><span style="color:#f92672">(</span>x<span style="color:#f92672">);</span>
<span style="color:#75715e">// 获取数组长度
</span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> len <span style="color:#f92672">=</span> temp<span style="color:#f92672">.</span><span style="color:#a6e22e">size</span><span style="color:#f92672">();</span>
<span style="color:#75715e">// 获取数组temp中的第i个元素，
</span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> val <span style="color:#f92672">=</span> temp<span style="color:#f92672">.</span><span style="color:#a6e22e">get</span><span style="color:#f92672">(</span>i<span style="color:#f92672">);</span>	<span style="color:#75715e">// 不能使用temp[i];
</span><span style="color:#75715e">// 定义一个二维数组
</span><span style="color:#75715e"></span>List<span style="color:#f92672">&lt;</span>List<span style="color:#f92672">&lt;</span>Integer<span style="color:#f92672">&gt;&gt;</span> result <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> ArrayList<span style="color:#f92672">&lt;&gt;();</span>
<span style="color:#75715e">// 将temp加入result
</span><span style="color:#75715e"></span>result<span style="color:#f92672">.</span><span style="color:#a6e22e">add</span><span style="color:#f92672">(</span><span style="color:#66d9ef">new</span> ArrayList<span style="color:#f92672">&lt;&gt;(</span>temp<span style="color:#f92672">));</span>
</code></pre></div><h4 id="4输出和integer">4、输出和Integer</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#75715e">// 输出变量i和变量j的值
</span><span style="color:#75715e"></span>System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span><span style="color:#f92672">.</span><span style="color:#a6e22e">println</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;i,j: &#34;</span><span style="color:#f92672">+</span>i<span style="color:#f92672">+</span>j<span style="color:#f92672">);</span>

<span style="color:#75715e">// 关于BigInteger    https://www.liaoxuefeng.com/wiki/1252599548343744/1279767986831393
</span><span style="color:#75715e"></span>Integer max <span style="color:#f92672">=</span> Integer<span style="color:#f92672">.</span><span style="color:#a6e22e">MAX_VALUE</span><span style="color:#f92672">;</span>	<span style="color:#75715e">// 整型的最大值
</span><span style="color:#75715e"></span>Integer min <span style="color:#f92672">=</span> Integer<span style="color:#f92672">.</span><span style="color:#a6e22e">MIN_VALUE</span><span style="color:#f92672">;</span>	<span style="color:#75715e">// 整型的最小值
</span></code></pre></div><h4 id="5字符串">5、字符串</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">String str <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;abcde&#34;</span><span style="color:#f92672">;</span>		<span style="color:#75715e">// 创建一个字符串
</span><span style="color:#75715e"></span><span style="color:#66d9ef">char</span> temp <span style="color:#f92672">=</span> str<span style="color:#f92672">.</span><span style="color:#a6e22e">charAt</span><span style="color:#f92672">(</span>i<span style="color:#f92672">);</span>	<span style="color:#75715e">// 第i个字符
</span><span style="color:#75715e"></span>str <span style="color:#f92672">+=</span> <span style="color:#e6db74">&#34;fgh&#34;</span><span style="color:#f92672">;</span>				<span style="color:#75715e">// 字符串连接
</span><span style="color:#75715e"></span>str<span style="color:#f92672">.</span><span style="color:#a6e22e">substring</span><span style="color:#f92672">(</span>i<span style="color:#f92672">,</span>j<span style="color:#f92672">);</span>			<span style="color:#75715e">// 获取[i,j)之间的字符
</span><span style="color:#75715e"></span>str<span style="color:#f92672">.</span><span style="color:#a6e22e">substring</span><span style="color:#f92672">(</span>i<span style="color:#f92672">);</span>			<span style="color:#75715e">// 获取[i,endl]之间的子串
</span></code></pre></div><h4 id="6hash表">6、hash表</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#75715e">// hash映射
</span><span style="color:#75715e"></span>Map<span style="color:#f92672">&lt;</span>Integer<span style="color:#f92672">,</span>Integer<span style="color:#f92672">&gt;</span> hash <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> HashMap<span style="color:#f92672">&lt;&gt;();</span>	<span style="color:#75715e">// 定义一个hash映射集
</span><span style="color:#75715e"></span>hash<span style="color:#f92672">.</span><span style="color:#a6e22e">put</span><span style="color:#f92672">(</span>key<span style="color:#f92672">,</span> val<span style="color:#f92672">);</span>		<span style="color:#75715e">// 将键值对&lt;key, val&gt;加入到hash映射中
</span><span style="color:#75715e"></span>hash<span style="color:#f92672">.</span><span style="color:#a6e22e">get</span><span style="color:#f92672">(</span>key<span style="color:#f92672">);</span>			<span style="color:#75715e">// 获取key对应的val
</span><span style="color:#75715e"></span>hash<span style="color:#f92672">.</span><span style="color:#a6e22e">containsKey</span><span style="color:#f92672">(</span>key<span style="color:#f92672">);</span>	<span style="color:#75715e">// 判断key是否已存在hash映射中
</span></code></pre></div><h2 id="数组">数组</h2>
<h3 id="1寻找数组的中心索引">1、寻找数组的中心索引</h3>
<blockquote>
<p>给你一个整数数组 nums，请编写一个能够返回数组 “中心下标” 的方法。</p>
<p>数组 中心下标 是数组的一个下标，其左侧所有元素相加的和等于右侧所有元素相加的和。</p>
<p>如果数组不存在中心下标，返回 -1 。如果数组有多个中心下标，应该返回最靠近左边的那一个。</p>
<p>注意：中心下标可能出现在数组的两端。</p>
</blockquote>
<p><strong><code>前缀和</code> <code>preSum</code></strong></p>
<p><a href="https://leetcode-cn.com/problems/find-pivot-index/solution/xiang-jie-presumhao-de-ti-jie-ying-gai-k-mzsg/">https://leetcode-cn.com/problems/find-pivot-index/solution/xiang-jie-presumhao-de-ti-jie-ying-gai-k-mzsg/</a></p>
<h3 id="2搜索插入位置">2、搜索插入位置</h3>
<blockquote>
<p>给定一个排序数组和一个目标值，在数组中找到目标值，并返回其索引。如果目标值不存在于数组中，返回它将会被按顺序插入的位置。</p>
<p>你可以假设数组中无重复元素。</p>
</blockquote>
<p><strong><code>二分查找</code></strong></p>
<p><a href="https://leetcode-cn.com/problems/search-insert-position/solution/te-bie-hao-yong-de-er-fen-cha-fa-fa-mo-ban-python-/">https://leetcode-cn.com/problems/search-insert-position/solution/te-bie-hao-yong-de-er-fen-cha-fa-fa-mo-ban-python-/</a></p>
<h2 id="二维数组简介">二维数组简介</h2>
<h3 id="1零矩阵">1、零矩阵</h3>
<blockquote>
<p>编写一种算法，若M × N矩阵中某个元素为0，则将其所在的行与列清零。</p>
</blockquote>
<p><a href="https://leetcode-cn.com/problems/zero-matrix-lcci/solution/liang-ci-bian-li-si-lu-jian-dan-chao-996-r5tw/">https://leetcode-cn.com/problems/zero-matrix-lcci/solution/liang-ci-bian-li-si-lu-jian-dan-chao-996-r5tw/</a></p>
		</div>
		<footer class="post__footer">
			
<div class="post__tags tags clearfix">
	<svg class="tags__badge icon icon-tag" width="16" height="16" viewBox="0 0 32 32"><path d="M32 19c0 1-1 2-1 2L21 31s-1 1-2 1-2-1-2-1L2 16c-1-1-1.4-2-1.4-2S0 12.5 0 11V3C0 1.5.8.8.8.8S1.5 0 3 0h8c1.5 0 3 .6 3 .6S15 1 16 2l15 15s1 1 1 2zM7 10a3 3 0 1 0 0-6 3 3 0 0 0 0 6z"/></svg>
	<ul class="tags__list">
		<li class="tags__item">
			<a class="tags__link btn" href="/tags/c/" rel="tag">C</a>
		</li>
		<li class="tags__item">
			<a class="tags__link btn" href="/tags/java/" rel="tag">Java</a>
		</li>
		<li class="tags__item">
			<a class="tags__link btn" href="/tags/c&#43;&#43;/" rel="tag">C&#43;&#43;</a>
		</li>
	</ul>
</div>
		</footer>
	</article>
</main>

<div class="authorbox clearfix">
	<figure class="authorbox__avatar">
		<img alt="Team317 avatar" src="/img/profile.png" class="avatar" height="90" width="90">
	</figure>
	<div class="authorbox__header">
		<span class="authorbox__name">About Team317</span>
	</div>
	<div class="authorbox__description">
		所有事物都在发展，每个人都在不断成长，我也在向前推进着。
	</div>
</div>

<nav class="pager flex">
	<div class="pager__item pager__item--prev">
		<a class="pager__link" href="/%E7%AE%97%E6%B3%95/%E8%AE%A1%E6%95%B0%E4%BA%8C%E8%BF%9B%E5%88%B6%E4%B8%AD1%E7%9A%84%E4%BD%8D%E6%95%B0/" rel="prev">
			<span class="pager__subtitle">«&thinsp;Previous</span>
			<p class="pager__title">计数二进制中1的位数</p>
		</a>
	</div>
</nav>

<section class="comments">
	<div id="disqus_thread"></div>
<script type="application/javascript">
    var disqus_config = function () {
    
    
    
    };
    (function() {
        if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
            document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
            return;
        }
        var d = document, s = d.createElement('script'); s.async = true;
        s.src = '//' + "team317-disqus" + '.disqus.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</section>


			</div>
			<aside class="sidebar"><div class="widget-search widget">
	<form class="widget-search__form" role="search" method="get" action="https://google.com/search">
		<label>
			<input class="widget-search__field" type="search" placeholder="SEARCH…" value="" name="q" aria-label="SEARCH…">
		</label>
		<input class="widget-search__submit" type="submit" value="Search">
		<input type="hidden" name="sitesearch" value="https://team317.gitee.io/" />
	</form>
</div>
<div class="widget-categories widget">
	<h4 class="widget__title">Categories</h4>
	<div class="widget__content">
		<ul class="widget__list">
			<li class="widget__item">
				<a class="widget__link" href="/categories/c&#43;&#43;%E7%AC%94%E8%AE%B0/">C&#43;&#43;笔记</a>&nbsp;
				<span class="widget__counter widget__counter--bubble">1</span>
				</li>
			<li class="widget__item">
				<a class="widget__link" href="/categories/c%E8%AF%AD%E8%A8%80%E7%AC%94%E8%AE%B0/">C语言笔记</a>&nbsp;
				<span class="widget__counter widget__counter--bubble">3</span>
				</li>
			<li class="widget__item">
				<a class="widget__link" href="/categories/htmlcssjs/">html&amp;css&amp;js</a>&nbsp;
				<span class="widget__counter widget__counter--bubble">1</span>
				</li>
			<li class="widget__item">
				<a class="widget__link" href="/categories/java%E7%AC%94%E8%AE%B0/">Java笔记</a>&nbsp;
				<span class="widget__counter widget__counter--bubble">2</span>
				</li>
			<li class="widget__item">
				<a class="widget__link" href="/categories/python%E7%AC%94%E8%AE%B0/">python笔记</a>&nbsp;
				<span class="widget__counter widget__counter--bubble">8</span>
				</li>
			<li class="widget__item">
				<a class="widget__link" href="/categories/%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86/">数据处理</a>&nbsp;
				<span class="widget__counter widget__counter--bubble">2</span>
				</li>
			<li class="widget__item">
				<a class="widget__link" href="/categories/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E5%92%8C%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">机器学习和深度学习</a>&nbsp;
				<span class="widget__counter widget__counter--bubble">1</span>
				</li>
			<li class="widget__item">
				<a class="widget__link" href="/categories/%E7%AE%97%E6%B3%95/">算法</a>&nbsp;
				<span class="widget__counter widget__counter--bubble">2</span>
				</li>
			<li class="widget__item">
				<a class="widget__link" href="/categories/%E7%BC%96%E7%A8%8B%E6%95%A3%E8%AE%B0/">编程散记</a>&nbsp;
				<span class="widget__counter widget__counter--bubble">6</span>
				</li>
		</ul>
	</div>
</div>
<div class="widget-taglist widget">
	<h4 class="widget__title">Tags</h4>
	<div class="widget__content">
		<a class="widget-taglist__link widget__link btn" href="/tags/c/" title="C">C (4)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/c&#43;&#43;/" title="C&#43;&#43;">C&#43;&#43; (2)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/git/" title="git">git (1)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/html/" title="html">html (1)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/java/" title="Java">Java (3)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/json/" title="json">json (1)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/linux/" title="linux">linux (1)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/mysql/" title="mysql">mysql (1)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/python/" title="python">python (8)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/vim/" title="vim">vim (1)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/%E4%BD%8D%E8%BF%90%E7%AE%97/" title="位运算">位运算 (1)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/%E5%8A%A8%E6%80%81%E5%86%85%E5%AD%98/" title="动态内存">动态内存 (1)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/%E5%A4%9A%E7%BA%BF%E7%A8%8B/" title="多线程">多线程 (1)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/%E5%BC%82%E6%AD%A5/" title="异步">异步 (1)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/" title="机器学习">机器学习 (1)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" title="正则表达式">正则表达式 (2)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/%E6%B1%87%E7%BC%96/" title="汇编">汇编 (1)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/%E7%88%AC%E8%99%AB/" title="爬虫">爬虫 (3)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/%E7%BD%91%E7%BB%9C/" title="网络">网络 (1)</a>
		<a class="widget-taglist__link widget__link btn" href="/tags/%E8%99%9A%E6%8B%9F%E6%9C%BA/" title="虚拟机">虚拟机 (1)</a>
	</div>
</div>
<div class="widget-social widget">
	<h4 class="widget-social__title widget__title">Social</h4>
	<div class="widget-social__content widget__content">
		<div class="widget-social__item widget__item">
			<a class="widget-social__link widget__link btn" title="GitHub" rel="noopener noreferrer" href="https://github.com/Tom-DaLin" target="_blank">
				<svg class="widget-social__link-icon icon icon-github" width="24" height="24" viewBox="0 0 384 374"><path d="m192 0c-106.1 0-192 85.8-192 191.7 0 84.7 55 156.6 131.3 181.9 9.6 1.8 13.1-4.2 13.1-9.2 0-4.6-.2-16.6-.3-32.6-53.4 11.6-64.7-25.7-64.7-25.7-8.7-22.1-21.3-28-21.3-28-17.4-11.9 1.3-11.6 1.3-11.6 19.3 1.4 29.4 19.8 29.4 19.8 17.1 29.3 44.9 20.8 55.9 15.9 1.7-12.4 6.7-20.8 12.2-25.6-42.6-4.8-87.5-21.3-87.5-94.8 0-20.9 7.5-38 19.8-51.4-2-4.9-8.6-24.3 1.9-50.7 0 0 16.1-5.2 52.8 19.7 15.3-4.2 31.7-6.4 48.1-6.5 16.3.1 32.7 2.2 48.1 6.5 36.7-24.8 52.8-19.7 52.8-19.7 10.5 26.4 3.9 45.9 1.9 50.7 12.3 13.4 19.7 30.5 19.7 51.4 0 73.7-44.9 89.9-87.7 94.6 6.9 5.9 13 17.6 13 35.5 0 25.6-.2 46.3-.2 52.6 0 5.1 3.5 11.1 13.2 9.2 76.2-25.5 131.2-97.3 131.2-182 0-105.9-86-191.7-192-191.7z"/></svg>
				<span>GitHub</span>
			</a>
		</div>
		<div class="widget-social__item widget__item">
			<a class="widget-social__link widget__link btn" title="Email" href="mailto:2629757717@qq.com">
				<svg class="widget-social__link-icon icon icon-mail" width="24" height="24" viewBox="0 0 416 288"><path d="m0 16v256 16h16 384 16v-16-256-16h-16-384-16zm347 16-139 92.5-139-92.5zm-148 125.5 9 5.5 9-5.5 167-111.5v210h-352v-210z"/></svg>
				<span>2629757717@qq.com</span>
			</a>
		</div>

		
		<div class="widget-social__item widget__item">
			<a class="widget-social__link widget__link btn" title="CSDN" rel="noopener noreferrer" href="https://blog.csdn.net/GodNotAMen?type=blog" target="_blank">
					<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1646737979982" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1433" width="30" height="16" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><style type="text/css"></style></defs><path d="M512 1024C229.2224 1024 0 794.7776 0 512 0 229.2224 229.2224 0 512 0c282.7776 0 512 229.2224 512 512 0 282.7776-229.2224 512-512 512z m17.066667-413.525333c34.850133 4.352 68.778667 5.12 102.741333 2.0992 23.04-2.048 44.817067-8.362667 64.170667-21.9136 38.212267-26.794667 49.783467-85.1968 24.251733-123.050667-14.626133-21.7088-36.8128-30.344533-60.757333-35.498667-35.054933-7.543467-70.4512-5.751467-105.847467-3.413333-5.666133 0.3584-6.7584 3.072-7.236267 8.209067-3.072 32.682667-6.536533 65.314133-9.813333 97.962666-2.5088 24.814933-4.932267 49.629867-7.509333 75.605334z m53.4016-33.928534c1.962667-20.906667 3.6352-39.338667 5.4272-57.770666 1.553067-15.906133 3.413333-31.778133 4.727466-47.701334 0.3584-4.283733 1.553067-6.656 5.956267-6.382933 15.616 1.041067 31.709867 0.034133 46.728533 3.652267 36.488533 8.823467 48.725333 54.306133 23.3472 83.029333-15.8208 17.902933-36.7616 23.586133-59.255466 25.088-8.465067 0.546133-17.015467 0.085333-26.9312 0.085333zM512 434.295467c-2.184533-0.648533-3.5328-1.1776-4.932267-1.4336-37.717333-6.877867-75.690667-8.328533-113.646933-2.816-20.974933 3.037867-41.0112 9.489067-57.480533 23.330133-22.9888 19.319467-21.640533 46.848 4.4032 62.0032 13.056 7.594667 28.023467 12.509867 42.5984 17.288533 14.08 4.608 28.996267 6.826667 43.144533 11.264 12.5952 3.925333 14.011733 14.318933 3.584 22.306134-3.345067 2.56-7.441067 5.085867-11.537067 5.751466-11.195733 1.826133-22.698667 4.386133-33.826133 3.566934-24.098133-1.774933-48.042667-5.461333-72.5504-8.430934-1.365333 10.615467-2.935467 23.0912-4.5568 35.9424 4.181333 1.365333 7.68 2.730667 11.264 3.618134 33.9456 8.4992 68.386133 9.608533 102.912 5.12 20.087467-2.6112 39.4752-7.901867 56.695467-19.029334 28.603733-18.4832 36.693333-57.1904-4.676267-75.383466-14.506667-6.382933-30.190933-10.410667-45.482667-15.086934-11.4176-3.4816-23.313067-5.614933-34.525866-9.5232-9.7792-3.413333-11.144533-12.202667-3.037867-18.397866 4.6592-3.549867 10.717867-6.997333 16.384-7.3728a480.853333 480.853333 0 0 1 53.384533-0.853334c15.377067 0.699733 30.651733 3.549867 46.4896 5.5296L512 434.295467z m257.143467 2.048L750.933333 614.2976h54.152534c4.778667-45.636267 9.710933-90.7264 14.062933-135.8848 0.6144-6.365867 2.3552-8.840533 8.686933-9.0112 11.434667-0.273067 22.8864-1.979733 34.286934-1.570133 23.722667 0.853333 42.3936 9.728 38.4 43.264-2.901333 24.2688-5.597867 48.571733-8.2432 72.874666-1.092267 10.069333-1.826133 20.189867-2.730667 30.4128h55.330133c3.584-35.259733 7.9872-70.058667 10.496-104.994133 3.413333-47.4624-17.7664-73.3184-64.682666-80.213333-40.96-6.007467-81.339733-0.341333-121.5488 7.133866z m-483.498667 134.6048c-8.738133 1.297067-16.384 2.798933-24.098133 3.4816-25.6512 2.235733-51.319467 3.9424-76.305067-4.266667-13.909333-4.590933-24.6784-12.578133-29.7984-25.9584-7.901867-20.701867 0.887467-47.104 19.831467-60.3136 17.373867-12.117333 37.717333-15.9232 58.453333-15.9232 22.545067-0.017067 45.090133 2.423467 68.232533 3.84L307.2 432.298667c-15.069867-1.723733-29.4912-3.925333-43.997867-4.9152-41.0112-2.798933-80.64 2.6112-117.469866 20.462933-30.020267 14.557867-52.053333 36.010667-58.6752 68.130133-7.850667 38.144 11.537067 69.495467 51.7632 85.845334 19.1488 7.765333 39.287467 12.509867 60.0064 12.5952 24.746667 0.1024 49.493333-1.570133 74.205866-2.952534 3.106133-0.170667 8.311467-2.901333 8.669867-5.034666 1.979733-11.554133 2.730667-23.278933 3.9424-35.464534z" fill="#DD1700" p-id="1434"></path></svg>
				<span>CSDN</span>
			</a>
		</div>
		
		<div class="widget-social__item widget__item">
			<a class="widget-social__link widget__link btn" title="知乎" rel="noopener noreferrer" href="https://www.zhihu.com/people/qiu-shui-wei-lan-95/posts" target="_blank">
					<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1646703887028" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2079" width="30" height="16" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><style type="text/css"></style></defs><path d="M576.8 807.52h57.28l20.8 72.48 100.8-72.48h141.92V229.28H576.8z m67.84-513.92H832v448h-66.24l-85.12 64.96-18.56-64.96h-17.44zM126.4 884.48a149.44 149.44 0 0 0 123.84-10.4c60.96-36 105.92-194.56 105.92-194.56l144 177.44s13.12-84.48-2.24-108.32-99.04-119.84-99.04-119.84l-36.64 32 26.08-104.96H544s0-61.76-30.56-65.28-125.44 0-125.44 0v-192H528s-1.6-64-28.8-64H270.56l35.52-104.64s-57.6 3.36-77.92 39.36-86.4 221.6-86.4 221.6 21.92 10.24 59.2-17.28a147.68 147.68 0 0 0 49.28-75.52l67.84-3.36L320 491.2s-116.96-1.76-140.64 0-37.28 65.28-37.28 65.28H320s-15.2 108.16-60.96 187.2-132.64 140.8-132.64 140.8z" fill="#49C0FB" p-id="2080"></path></svg>
				<span>知乎</span>
			</a>
		</div>
		
		<div class="widget-social__item widget__item">
			<a class="widget-social__link widget__link btn" title="Gitee" rel="noopener noreferrer" href="https://gitee.com/team317" target="_blank">
					<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1647517716559" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1578" width="30" height="16" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><style type="text/css"></style></defs><path d="M512 1024C229.222 1024 0 794.778 0 512S229.222 0 512 0s512 229.222 512 512-229.222 512-512 512z m259.149-568.883h-290.74a25.293 25.293 0 0 0-25.292 25.293l-0.026 63.206c0 13.952 11.315 25.293 25.267 25.293h177.024c13.978 0 25.293 11.315 25.293 25.267v12.646a75.853 75.853 0 0 1-75.853 75.853h-240.23a25.293 25.293 0 0 1-25.267-25.293V417.203a75.853 75.853 0 0 1 75.827-75.853h353.946a25.293 25.293 0 0 0 25.267-25.292l0.077-63.207a25.293 25.293 0 0 0-25.268-25.293H417.152a189.62 189.62 0 0 0-189.62 189.645V771.15c0 13.977 11.316 25.293 25.294 25.293h372.94a170.65 170.65 0 0 0 170.65-170.65V480.384a25.293 25.293 0 0 0-25.293-25.267z" fill="#C71D23" p-id="1579"></path></svg>
				<span>Gitee</span>
			</a>
		</div>
		
	</div>
</div>
</aside>
		</div>
		<footer class="footer">
	<div class="container footer__container flex">
		
		<div class="footer__copyright">
			&copy; 2022 Team317.
			<span class="footer__copyright-credits">Generated with <a href="https://gohugo.io/" rel="nofollow noopener" target="_blank">Hugo</a> and <a href="https://github.com/Vimux/Mainroad/" rel="nofollow noopener" target="_blank">Mainroad</a> theme.</span>
		</div>
	</div>
</footer>
	</div>
<script async defer src="/js/menu.js"></script>
</body>
</html>